﻿// **************************************************
// 以下代码由代码生成器生成。
// 请不要修改该文件，以避免您的修改被代码生成器覆盖。
// 如需扩展方法，可新建文件，利用partial类特性来实现。
// **************************************************
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace {{namespace}}
{
    [Authorize]
    public partial class {{class_name}}AppService : {{project_group_name}}AppService, I{{class_name}}AppService
    {
        protected I{{class_name}}Repository {{class_name}}Repository => LazyServiceProvider.LazyGetRequiredService<I{{class_name}}Repository>();
        protected {{class_name}}Manager {{class_name}}Manager => LazyServiceProvider.LazyGetRequiredService<{{class_name}}Manager>();

        [Authorize({{project_group_name}}Permissions.{{class_name}}.Query)]
        public async Task<{{class_name}}Dto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<{{class_name}}, {{class_name}}Dto>(
                await {{class_name}}Repository.GetAsync(id, true)
            );
        }

        [Authorize({{project_group_name}}Permissions.{{class_name}}.Query)]
        public async Task<{{class_name}}Dto> FindAsync(string filter)
        {
            var entity = await {{class_name}}Repository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<{{class_name}}, {{class_name}}Dto>(entity);
        }

        [Authorize({{project_group_name}}Permissions.{{class_name}}.Query)]
        public async Task<PagedResultDto<{{class_name}}Dto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await {{class_name}}Repository.GetCountAsync(true);
            var list = await {{class_name}}Repository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<{{class_name}}Dto>(
                totalCount,
                ObjectMapper.Map<List<{{class_name}}>, List<{{class_name}}Dto>>(list));
        }

        [Authorize({{project_group_name}}Permissions.{{class_name}}.Query)]
        public async Task<PagedResultDto<{{class_name}}Dto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<{{class_name}}, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<{{class_name}}>(input.FilterGroup);
            }

            var totalCount = await {{class_name}}Repository.GetCountAsync(expression, true);
            var list = await {{class_name}}Repository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<{{class_name}}Dto>(
                totalCount,
                ObjectMapper.Map<List<{{class_name}}>, List<{{class_name}}Dto>>(list));
        }

        [Authorize({{project_group_name}}Permissions.{{class_name}}.Query)]
        public virtual async Task<ListResultDto<{{class_name}}Dto>> GetChildrenAsync(Guid? parentId, bool recursive = false)
        {
            var list = ObjectMapper.Map<List<{{class_name}}>, List<{{class_name}}Dto>>(
                await {{class_name}}Repository.GetChildrenAsync(parentId, false, recursive)
            ).OrderBy(x => x.Code).ToList();

            return new ListResultDto<{{class_name}}Dto>(list);
        }

        [Authorize({{project_group_name}}Permissions.{{class_name}}.Create)]
        public async Task<{{class_name}}Dto> CreateAsync({{class_name}}CreateDto input)
        {
            var entity = ObjectMapper.Map<{{class_name}}CreateDto, {{class_name}}>(input);
            input.MapExtraPropertiesTo(entity);

            await {{class_name}}Manager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize({{project_group_name}}Permissions.{{class_name}}.Update)]
        public async Task<{{class_name}}Dto> UpdateAsync(Guid id, {{class_name}}UpdateDto input)
        {
            var entity = await {{class_name}}Repository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            input.MapExtraPropertiesTo(entity);

            await {{class_name}}Manager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize({{project_group_name}}Permissions.{{class_name}}.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await {{class_name}}Manager.DeleteAsync(id);
        }
    }
}
